package com.example.securityconfig.entity;


import com.example.securityconfig.entity.enums.ExceptionCodeEnum;
import lombok.Data;

import java.io.Serializable;

@Data
public class Result<T> implements Serializable {

    private String message;
    private int retCode;
    private T data;

    public Result(String message, int retCode, T data) {
        this.message = message;
        this.retCode = retCode;
        this.data = data;
    }

    private Result(Integer code, String message) {
        this.retCode = code;
        this.message = message;
        this.data = null;
    }

    public static Result<?> success(Object data) {
        return new Result<>("成功", 200, data);
    }

    public static Result<?> success(String message) {
        return new Result<>(message, 200, null);
    }

    public static Result<?> success() {
        return new Result<>("成功", 200, null);
    }


    /**
     * 通用错误返回，传入指定的错误枚举
     *
     * @param exceptionCodeEnum
     * @return
     */
    public static <T> Result<T> error(ExceptionCodeEnum exceptionCodeEnum) {
        return new Result<>(exceptionCodeEnum.getCode(), exceptionCodeEnum.getMsg());
    }

    /**
     * 通用错误返回，传入指定的错误枚举，但支持覆盖message
     *
     * @param exceptionCodeEnum
     * @param msg
     * @return
     */
    public static <T> Result<T> error(ExceptionCodeEnum exceptionCodeEnum, String msg) {
        return new Result<>(exceptionCodeEnum.getCode(), msg);
    }

}